package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.Result;
import com.itheima.service.MemberService;
import com.itheima.service.OrderService;
import com.itheima.service.ReportService;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("report")
public class ReportController {

    @Reference
    private MemberService memberService;

    @Reference
    private OrderService orderService;

    @Reference
    private ReportService reportService;

    @RequestMapping("getMemberReport")
    public Result getMemberReport(@RequestBody Map map){

        try {
            String startDate = (String) map.get("startDate");
            String endDate = (String) map.get("endDate");
            Map memberReport = memberService.getMemberReport(startDate, endDate);

            return new Result(true, MessageConstant.GET_MEMBER_NUMBER_REPORT_SUCCESS,memberReport);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_MEMBER_NUMBER_REPORT_FAIL);
        }

    }

    /**
     * 获取下单的套餐占比数据
     */
    @RequestMapping("getSetmealReport")
    public Result getSetmealReport(){
        try {
            Map map = orderService.getSetmealReport();
            return new Result(true,MessageConstant.GET_SETMEAL_COUNT_REPORT_SUCCESS,map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.GET_SETMEAL_COUNT_REPORT_FAIL);
        }
    }

    /**
     * 查询运营数据
     */
    @RequestMapping("getBusinessReport")
    public Result getBusinessReport(){
        try {
            Map map = reportService.getBusinessReport();
            return new Result(true,MessageConstant.GET_BUSINESS_REPORT_SUCCESS,map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.GET_BUSINESS_REPORT_FAIL);
        }

    }

    /**
     * 将运营数据填入excel模板后下载
     */
    @RequestMapping("exportBusinessReport")
    public void exportBusinessReport(HttpSession session, HttpServletResponse response){

        try {

            Map result = reportService.getBusinessReport();
            //取出返回结果数据，准备将报表数据写入到Excel文件中
            String reportDate = (String) result.get("reportDate");
            Integer todayNewMember = (Integer) result.get("todayNewMember");
            Integer totalMember = (Integer) result.get("totalMember");
            Integer thisWeekNewMember = (Integer) result.get("thisWeekNewMember");
            Integer thisMonthNewMember = (Integer) result.get("thisMonthNewMember");
            Integer todayOrderNumber = (Integer) result.get("todayOrderNumber");
            Integer thisWeekOrderNumber = (Integer) result.get("thisWeekOrderNumber");
            Integer thisMonthOrderNumber = (Integer) result.get("thisMonthOrderNumber");
            Integer todayVisitsNumber = (Integer) result.get("todayVisitsNumber");
            Integer thisWeekVisitsNumber = (Integer) result.get("thisWeekVisitsNumber");
            Integer thisMonthVisitsNumber = (Integer) result.get("thisMonthVisitsNumber");
            List<Map> hotSetmeal = (List<Map>) result.get("hotSetmeal");
            //获取模板文件路径
            String realPath = session.getServletContext().getRealPath("template")+ File.separator+"report_template.xlsx";

            //加载模板获取工作簿对象
            XSSFWorkbook workbook = new XSSFWorkbook(realPath);
            //获取工作表
            XSSFSheet sheet = workbook.getSheetAt(0);
            //获取行
            XSSFRow row = sheet.getRow(2);
            //给单元格填充数据
            row.getCell(5).setCellValue(reportDate);

            row = sheet.getRow(4);
            row.getCell(5).setCellValue(todayNewMember);
            row.getCell(7).setCellValue(totalMember);

            row = sheet.getRow(5);
            row.getCell(5).setCellValue(thisWeekNewMember);
            row.getCell(7).setCellValue(thisMonthNewMember);

            row = sheet.getRow(7);
            row.getCell(5).setCellValue(todayOrderNumber);
            row.getCell(7).setCellValue(todayVisitsNumber);

            row = sheet.getRow(8);
            row.getCell(5).setCellValue(thisWeekOrderNumber);
            row.getCell(7).setCellValue(thisWeekVisitsNumber);

            row = sheet.getRow(9);
            row.getCell(5).setCellValue(thisMonthOrderNumber);
            row.getCell(7).setCellValue(thisMonthVisitsNumber);


            int num = 12;
            for (Map map : hotSetmeal) {

                String name = (String) map.get("name");
                Long setmealCount = (Long) map.get("setmeal_count");
                BigDecimal proportion =  (BigDecimal)map.get("proportion");

                row = sheet.getRow(num);

                row.getCell(4).setCellValue(name);
                row.getCell(5).setCellValue(setmealCount);
                row.getCell(6).setCellValue(proportion.toString());
                num++;
            }

            //设置下载文件的格式
            response.setContentType("application/vnd.ms-excel");

            //通知浏览器以附件形式下载
            response.setHeader("content-Disposition","attachment;filename=report.xlsx");

            ServletOutputStream out = response.getOutputStream();

            workbook.write(out);

            out.flush();//刷出缓冲区
            out.close();
            workbook.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    /**
     * 会员性别饼图
     * */
    @RequestMapping("getMemberSexs")
    public Result getMemberSexs(){
        try {
            Map map = memberService.getMemberSexs();
            return new Result(true,MessageConstant.GET_MEMBER_NUMBER_REPORT_SUCCESS,map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.GET_MEMBER_NUMBER_REPORT_FAIL);
        }
    }
    /**
     * 会员年龄饼图
     * */
    @RequestMapping("getMemberAges")
    public Result getMemberAges(){
        try {
            Map map =memberService.getMemberAges();
            return new Result(true,MessageConstant.GET_MEMBER_NUMBER_REPORT_FAIL,map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.GET_MEMBER_NUMBER_REPORT_FAIL);
        }
    }
}
